"use strict";

/**
 * 
 * @desc   判断是否为邮箱地址
 * @param  {String}  str
 * @return {Boolean} 
 */
function isEmail(str) {
  return /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(str);
}
/**
 * 
 * @desc  判断是否为身份证号
 * @param  {String|Number} str 
 * @return {Boolean}
 */


function isIdCard(str) {
  return /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/.test(str);
}
/**
 * 
 * @desc   判断是否为手机号
 * @param  {String|Number} str 
 * @return {Boolean} 
 */


function isPhoneNum(str) {
  return /^(\+?0?86\-?)?1[3456789]\d{9}$/.test(str);
}
/**
 * 
 * @desc   判断是否为URL地址
 * @param  {String} str 
 * @return {Boolean}
 */


function isUrl(str) {
  return /[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/i.test(str);
}
/**
 * 
 * @desc   千分位分隔符
 * @param  {Number} num 
 * @return {NumStr}
 */


function toThousands(num) {
  if (!num) {
    return;
  }

  num = num + '';
  var reg = /\d{1,3}(?=(\d{3})+$)/g;
  var intNum = '';
  var decimalNum = '';

  if (num.indexOf('.') > -1) {
    intNum = num.substring(0, num.indexOf('.'));
    decimalNum = num.substring(num.indexOf('.') + 1, num.length);
    return (intNum + '').replace(reg, '$&,') + '.' + decimalNum;
  } else {
    return (num + '').replace(reg, '$&,');
  }
}

var regx = {
  isEmail: isEmail,
  isIdCard: isIdCard,
  isPhoneNum: isPhoneNum,
  isUrl: isUrl,
  toThousands: toThousands
};
module.exports = regx;